package com.wanshi.controller;


import com.wanshi.client.OrdersClient;
import com.wanshi.entity.Orders;
import com.wanshi.entity.User;
import com.wanshi.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 顽石教育-www.wanshi.com
 * @since 2021-12-21
 */
@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
    private IUserService userService;

@Autowired
private OrdersClient ordersClient;


@GetMapping("/{id}")
public User findById(@PathVariable String id){

    return userService.getById(id);
}


    @GetMapping("/list")
    public List<User> list(){

        return userService.list();
    }


    @PostMapping
    public String addUser(@RequestBody User user){
    userService.save(user);
    return "添加成功";
    }


    //根据用户ID查询用户及对应的订单
    @GetMapping("/orders/{id}")
    public User findUserAndOrdersById(@PathVariable String id){

        User user = userService.getById(id);
        List<Orders> orders = ordersClient.findUserById(id);
        user.setOrders(orders);
        return user;
    }


}

